//
//  LZTopicCell.m
//  LZBS
//
//  Created by 郝庆 on 16/6/10.
//  Copyright © 2016年 haoqing. All rights reserved.
//

#import "LZTopicCell.h"
#import "LZTopic.h"
#import "LZComment.h"
#import "LZUser.h"
#import "LZTopicPictureView.h"
#import "LZTopicVoiceView.h"
#import "LZTopicVideoView.h"

@interface LZTopicCell ()
@property (weak, nonatomic) IBOutlet UIImageView *profileImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *createdAtLabel;
@property (weak, nonatomic) IBOutlet UILabel *text_label;
@property (weak, nonatomic) IBOutlet UIButton *dingButton;
@property (weak, nonatomic) IBOutlet UIButton *caiButton;
@property (weak, nonatomic) IBOutlet UIButton *repostButton;
@property (weak, nonatomic) IBOutlet UIButton *commentButton;
/** 最热评论-整体 */
@property (weak, nonatomic) IBOutlet UIView *topCmtView;
@property (weak, nonatomic) IBOutlet UILabel *topCmtContentLabel;

/* 中间控件 */
/** 图片控件 */
@property (nonatomic, weak) LZTopicPictureView *pictureView;
/** 声音控件 */
@property (nonatomic, weak) LZTopicVoiceView *voiceView;
/** 视频控件 */
@property (nonatomic, weak) LZTopicVideoView *videoView;

@end

@implementation LZTopicCell

#pragma mark - 懒加载
- (LZTopicPictureView *)pictureView
{
    if (!_pictureView) {
        LZTopicPictureView *pictureView = [LZTopicPictureView lz_viewFromXib];
        [self.contentView addSubview:pictureView];
        _pictureView = pictureView;
    }
    return _pictureView;
}

- (LZTopicVoiceView *)voiceView
{
    if (!_voiceView) {
        LZTopicVoiceView *voiceView = [LZTopicVoiceView lz_viewFromXib];
        [self.contentView addSubview:voiceView];
        _voiceView = voiceView;
    }
    return _voiceView;
}

- (LZTopicVideoView *)videoView
{
    if (!_videoView) {
        LZTopicVideoView *videoView = [LZTopicVideoView lz_viewFromXib];
        [self.contentView addSubview:videoView];
        _videoView = videoView;
    }
    return _videoView;
}
#pragma mark - 初始化
-(void)awakeFromNib
{
    self.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mainCellBackground"]];
}

- (IBAction)moreClick {
    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];

    [alertVc addAction:[UIAlertAction actionWithTitle:@"收藏" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        LZLog(@"点击了[收藏]按钮");
    }]];
    
    [alertVc addAction:[UIAlertAction actionWithTitle:@"举报" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        LZLog(@"点击了[举报]按钮");
    }]];
    
    [alertVc addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        LZLog(@"点击了[取消]按钮");
    }]];
    
    [self.window.rootViewController presentViewController:alertVc animated:YES completion:nil];
    
}

- (void)setTopic:(LZTopic *)topic
{
    _topic = topic;
    self.nameLabel.text = topic.name;
    
    self.text_label.text = topic.text;
    [self.text_label lz_setLineSpacing:10]; //设置行距
    
    self.createdAtLabel.text = topic.created_at;
    
    // 用户头像
    [self.profileImageView lz_setHeader:topic.profile_image];
    [self setUpButton:self.dingButton number:topic.ding placeholder:@"顶"];
    [self setUpButton:self.caiButton number:topic.cai placeholder:@"踩"];
    [self setUpButton:self.repostButton number:topic.repost placeholder:@"分享"];
    [self setUpButton:self.commentButton number:topic.comment placeholder:@"评论"];
    
    // 最热评论
    if (topic.top_cmt) { //有最热评论
        self.topCmtView.hidden = NO;
        self.topCmtContentLabel.text = [NSString stringWithFormat:@"%@ : %@",topic.top_cmt.user.username,topic.top_cmt.content];
    } else {
        self.topCmtView.hidden = YES;
    }
    
    
    // 中间内容
    if (topic.type == LZTopicTypeVideo) { // 视频
        self.videoView.hidden = NO;
        self.videoView.frame = topic.contentFrame;
        self.videoView.topic = topic;
        
        self.voiceView.hidden = YES;
        self.pictureView.hidden = YES;
    } else if (topic.type == LZTopicTypeVoice) { // 音频
        self.voiceView.hidden = NO;
        self.voiceView.frame = topic.contentFrame;
        self.voiceView.topic = topic;
        
        self.videoView.hidden = YES;
        self.pictureView.hidden = YES;
    } else if (topic.type == LZTopicTypePicture) { // 图片
        self.pictureView.hidden = NO;
        self.pictureView.frame = topic.contentFrame;
        self.pictureView.topic = topic;
        
        self.voiceView.hidden = YES;
        self.videoView.hidden = YES;
    } else if (topic.type == LZTopicTypeWord) { // 段子
        self.pictureView.hidden = YES;
        self.voiceView.hidden = YES;
        self.videoView.hidden = YES;
    }
}

/**
 *  设置按钮的数字
 */
- (void)setUpButton:(UIButton *)button number:(NSInteger)number placeholder:(NSString *)placeholder
{
    if (number >= 10000) {
        [button setTitle:[NSString stringWithFormat:@"%.1f万",number / 10000.0] forState:UIControlStateNormal];
    } else if (number > 0){
        [button setTitle:[NSString stringWithFormat:@"%zd",number] forState:UIControlStateNormal];
    }else{
        [button setTitle:placeholder forState:UIControlStateNormal];
    }
}

/**
 *  重写这个方法的目的: 能够拦截所有设置cell frame的操作
 */
- (void)setFrame:(CGRect)frame
{
    frame.size.height -= LZMargin;
    frame.origin.y += LZMargin;
    [super setFrame:frame];
}
@end
